package com.itheima.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.commons.utils.Constants;
import com.itheima.dubbo.api.*;
import com.itheima.model.domain.*;
import com.itheima.model.vo.CountsVo;
import com.itheima.model.vo.PageResult;
import com.itheima.model.vo.SettingsVo;
import com.itheima.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private BlackListApi blackListApi;
    @DubboReference
    private UserLikeApi userLikeApi;

    @DubboReference
    private UserApi userApi;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /*查询通知*/
    public SettingsVo settings(){
        SettingsVo settingsVo = new SettingsVo();
        //1.获取用户id
        Long userId = UserHolder.getUserId();
        settingsVo.setId(userId);
        //2.获取用户手机号
        String mobile = UserHolder.getMobile();
        settingsVo.setPhone(mobile);
        //.获取用户的陌生人问题
        Question question=questionApi.findByUserId(settingsVo.getId());
        if (question!=null){
            settingsVo.setStrangerQuestion(question.getTxt());
        }else {
            settingsVo.setStrangerQuestion("哈哈哈");
        }
        //获取用户的app通知开关数据
        Settings settings=settingsApi.findByUserId(settingsVo.getId());
        if(settings!=null){
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
        }
        return settingsVo;
    }
    /*设置陌生人问题，存在陌生人问题就更新，不存在就保存*/
    public void saveQuestion(String content){
        //1.获取用户id
        Long userId = UserHolder.getUserId();
        //.获取用户的陌生人问题
        Question question=questionApi.findByUserId(userId);
        if (question!=null){
            question.setTxt(content);
            questionApi.update(question);
            //存在就更新
        }else {
            //不存在就保存
            Question question1 = new Question();
            question1.setTxt(content);
            question1.setUserId(userId);
            questionApi.save(question1);
        }
    }
/*通知设置*/
    public void saveSettings(Map map) {
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean)  map.get("gonggaoNotification");
        //1.获取用户id
        Long userId = UserHolder.getUserId();
        //获取设置
        Settings settings = settingsApi.findByUserId(userId);
        if (settings==null){
            //不存在就保存
            settings=new Settings();
            settings.setUserId(userId);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.save(settings);
        }else {
            //存在就更新
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }
    }
    /*分页查询黑名单*/
    public PageResult blacklist(int page, int pagesize) {
        Long userId = UserHolder.getUserId();
       IPage<UserInfo> iPage= blackListApi.pageList(userId,page,pagesize);
        PageResult pageResult = new PageResult(page, pagesize, (long) Math.toIntExact(iPage.getTotal()), iPage.getRecords());
        return pageResult;
    }

    public void deleteBlackList(Long blackUserId) {
        Long userId = UserHolder.getUserId();
        blackListApi.delete(userId,blackUserId);
    }
    /**
     * 统计互相喜欢 喜欢 粉丝
     */
    public CountsVo counts() {
        return userLikeApi.counts(UserHolder.getUserId());
    }

    /**
     * 修改手机号 校验验证码
     *
     * @param verificationCode
     */
    public boolean checkCode(String verificationCode) {
        //1、从redis中获取下发的验证码
        String redisCode = redisTemplate.opsForValue().get(Constants.SMS_CODE + UserHolder.getMobile());
        if (verificationCode.equals(redisCode)) {
            //3、删除redis中的验证码
            redisTemplate.delete(Constants.SMS_CODE + UserHolder.getMobile());
            return true;
        }
        return false;
    }

    /**
     * 保存修改后的新号码
     * @param phone
     */
    public void saveNewPhone(String phone) {
        User user = new User();
        user.setId(UserHolder.getUserId());
        user.setMobile(phone);
        userApi.update(user);
    }
}
